Since C++11, it’s possible to declare the underlying type of an enum
, and like any type declaration, enum
declarations
can contain the const
or volatile
specifier. But because enum
values are named constants and cannot be
re-assigned, those specifiers are ignored by the compiler and are therefore useless.
This rule raises an issue if const
or volatile
is present in the declaration of the underlying type of an
enum
.
Noncompliant code example
enum class Color : const long int { // Noncompliant: Remove this "const" specifier.
Red = 0xff0000,
Green = 0x00ff00,
Blue = 0x0000ff
};
enum class Size : volatile char { // Noncompliant: Remove this "volatile" specifier.
Small = 's',
Big = 'b'
};
Compliant solution
enum class Color : long int {
Red = 0xff0000,
Green = 0x00ff00,
Blue = 0x0000ff
};
enum class Size : char {
Small = 's',
Big = 'b'
};